﻿using System;
using System.Data;
using System.Data.SqlClient;
using Pharmacy.Data;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;
using Pharmacy.Entities;

public partial class Admin_PreviewDrugDetail : System.Web.UI.Page
{
    DateTime tmpfromdate, tmptodate, fromdate, todate;
    int groupId = 0;
    string Drugid="";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["Id"] != null && Request.QueryString["Drugid"] != null && Request.QueryString["From"] != null && Request.QueryString["To"] != null)
            {
                groupId = int.Parse(Request.QueryString["Id"].ToString());
                Drugid = Request.QueryString["Drugid"].ToString();
                tmpfromdate = DateTime.Parse(Request.QueryString["From"].ToString());
                tmptodate = DateTime.Parse(Request.QueryString["To"].ToString());
                fromdate = new DateTime(tmpfromdate.Year, tmpfromdate.Month, tmpfromdate.Day, 0, 0, 0);
                todate = new DateTime(tmptodate.Year, tmptodate.Month, tmptodate.Day, 23, 59, 59);
                PreviewDrugs(groupId,Drugid, fromdate, todate);

            }
        }
    }
    private void PreviewDrugs(int Id,string drugid, DateTime from, DateTime to)
    {

        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Drug_EndAmountDetail";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 0;
        cmd.Parameters.Add("@GroupId", SqlDbType.Int).Value = Id;
        cmd.Parameters.Add("@DrugId", SqlDbType.NVarChar).Value =drugid ;
        cmd.Parameters.Add("@fromdate", SqlDbType.DateTime).Value = from;
        cmd.Parameters.Add("@todate", SqlDbType.DateTime).Value = to;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntableDrugs = ds.Tables[0];
        DataTable ntableGrn = ds.Tables[1];
        DSDrug.DrugsDataTable dtDrug = new DSDrug.DrugsDataTable();
        DSDrug.DrugsRow dtDrugsRow;
        for (int i = 0; i < ntableDrugs.Rows.Count; i++)
        {
            dtDrugsRow = dtDrug.NewDrugsRow();
            DataRow drw = ntableDrugs.Rows[i];
            dtDrugsRow.DrugName = drw["DrugName"].ToString();
            dtDrugsRow.Unit = drw["Unit"].ToString();
            dtDrugsRow.BeginQuantity = int.Parse(drw["BeginQuantity"].ToString());
            dtDrugsRow.InQuantity = int.Parse(drw["InQuantity"].ToString());
            dtDrugsRow.OutQuantity = int.Parse(drw["OutQuantity"].ToString());
            dtDrugsRow.AdjustQuantity = int.Parse(drw["AdjustQuantity"].ToString());
            dtDrugsRow.EndQuantity = int.Parse(drw["EndQuantity"].ToString());
            string remark = GetBatchLot(drw["DrugId"].ToString());
            dtDrugsRow.Remark = remark;
            dtDrug.AddDrugsRow(dtDrugsRow);
        }


        DSDrug.TitleDataTable dtTitle = new DSDrug.TitleDataTable();
        DSDrug.TitleRow dtTitleRow = dtTitle.NewTitleRow();
        dtTitleRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtTitleRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;
        dtTitleRow.Fromdate = from;
        dtTitleRow.Todate = to;
        //get prepareby from cookies
        var httpCookie = Request.Cookies["LoginUser"];
        WebUser id = DataRepository.WebUserProvider.GetByUserId(httpCookie.Value);
        dtTitleRow.PrepareBy = id.Name;
        dtTitle.AddTitleRow(dtTitleRow);

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSDrug_Drugs", dtDrug));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSDrug_Title", dtTitle));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSDrug_Grn", ds.Tables[1]));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSDrug_Prescription", ds.Tables[2]));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSDrug_StockOut", ds.Tables[3]));
        ReportViewer1.DataBind();

    }
    private string GetBatchLot(string drugId)
    {
        string result = "";
        DataSet ds = new DataSet();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select  dbo.GetBatchLot (@DrugId)";
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = 0;
        cmd.Parameters.Add("@DrugId", SqlDbType.NVarChar).Value = drugId;
        ds = DataRepository.Provider.ExecuteDataSet(cmd);
        DataTable ntable = ds.Tables[0];
        result = ntable.Rows[0]["Column1"].ToString();
        return result;
    }
}
